package com.mlnx.bluwifi.service.iml;

import com.mlnx.bluwifi.manager.SpoPolicy;
import com.mlnx.bluwifi.pojo.DataCache;
import com.mlnx.bluwifi.service.WifiBluService;
import com.mlnx.common.entity.Response;
import com.mlnx.common.utils.MyLog;
import com.mlnx.data.entity.BpInfo;
import com.mlnx.data.entity.SpoInfo;
import com.mlnx.data.service.BpDataService;
import com.mlnx.data.service.SpoDataService;
import com.mlnx.device.entity.Device;
import com.mlnx.service.base.properties.MutilDeviceProperties;
import org.shan.spring.rest.RestUtils;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.PostConstruct;

/**
 * Created by amanda.shan on 2019/11/18.
 */
public class WifiBluPushOldService extends BaseWifiBluService implements WifiBluService {

    MyLog log = MyLog.getLog(getClass());

    @Autowired
    private SpoDataService spoDataService;

    @Autowired
    private BpDataService bpDataService;

    @Autowired
    private MutilDeviceProperties mutilDeviceProperties;

    @PostConstruct
    public void init(){
        log.info("启动WifiBluPushOldService 老的推送服务");
    }

    @Override
    public void broadCastSpo(String deviceId, int spo, int heart) {

        if (SpoPolicy.getInstance().judge(deviceId, spo)) {
            log.info("收到mac:{}  血、氧数据:{}", deviceId, spo);
            SpoInfo spoInfo = new SpoInfo();
            spoInfo.setDeviceId(deviceId);
            spoInfo.setResultHeart(heart);
            spoInfo.setResultSPO(spo);
            spoInfo.setTime(System.currentTimeMillis());



//            if (spo > 100) {
//                SpoPolicy.getInstance().setSpoInfo(spoInfo,deviceId);
//                return;
//            }
            if (spo > 100) {
                DataCache dataCache= SpoPolicy.getInstance().getDataCache(deviceId);
                dataCache.setCurrentTime(System.currentTimeMillis());
                dataCache.setSpoInfo(spoInfo);
                SpoPolicy.getInstance().setDataCache(dataCache,deviceId);
                return;
            }

            deviceId = deviceId.toUpperCase();

            Device device = getDevice(deviceId);
            if (device != null) {
                Integer patientId = device.getPatientId();

                if (patientId == null || patientId.intValue() == 0) {
                    log.error("{} 设备未绑定病人", deviceId);
                } else {


                    spoInfo.setPatientId(patientId);
                    DataCache dataCache= SpoPolicy.getInstance().getDataCache(deviceId);
                    dataCache.setCurrentTime(System.currentTimeMillis());
                    dataCache.setSpoInfo(spoInfo);
                    SpoPolicy.getInstance().setDataCache(dataCache,deviceId);
//                    SpoPolicy.getInstance().setSpoInfo(spoInfo,deviceId);

                    spoDataService.save(spoInfo);

                    if (sendSpo(deviceId, spoInfo)) {
//                    log.info("mac:{}  保存和发送血压数据:{} 成功", spoMac, spo);
                    }
                }
            }
        }
    }

    @Override
    public void broadCastBp(String deviceId, int sbp, int dbp, int heart) {
        Device device = getDevice(deviceId);

        log.info("{} 收缩压:{}  舒张压:{}  心率:{}", deviceId, sbp, dbp, heart);

        if (device != null) {
            Integer patientId = device.getPatientId();

            if (patientId == null || patientId.intValue() == 0) {
                log.error("{} 设备未绑定病人", deviceId);
            } else {
                BpInfo bpInfo = new BpInfo();
                bpInfo.setSbp(sbp);
                bpInfo.setDbp(dbp);
                bpInfo.setHeart(heart);
                bpInfo.setPatientId(device.getPatientId());
                bpInfo.setPacketTime(System.currentTimeMillis());
                bpInfo.setDeviceId(deviceId);

                DataCache dataCache= SpoPolicy.getInstance().getDataCache(deviceId);
                dataCache.setCurrentTime(System.currentTimeMillis());
                dataCache.setBpInfo(bpInfo);
                SpoPolicy.getInstance().setDataCache(dataCache,deviceId);
                bpDataService.save(bpInfo);
                if (sendBp(deviceId, bpInfo)) {
//                    log.info("mac:{}  保存和发送血压数据:{} 成功", deviceId, bpInfo);
                }
            }
        }
    }

    private boolean sendSpo(String deviceId, SpoInfo spoInfo) {
        try {
            Response response = RestUtils.post(mutilDeviceProperties.getBluWifi().getPushUrlPrefix() + "/spo",
                    Response.class, spoInfo);

            if (response.isSucess()) {
                return true;
            } else {
                log.error("mac:{}  发送血氧到:{} 失败：response:{}", deviceId,
                        mutilDeviceProperties.getBluWifi().getPushUrlPrefix() + "/spo", response);
                return false;
            }
        } catch (Exception e) {
            log.error(e, "推送血氧失败");
        }
        return false;

    }

    private boolean sendBp(String deviceId, BpInfo bpInfo) {
        try {
            Response response = RestUtils.post(mutilDeviceProperties.getBluWifi().getPushUrlPrefix() + "/bp",
                    Response.class, bpInfo);

            if (response.isSucess()) {
                return true;
            } else {
                log.error("mac:{}  发送血压到:{} 失败：response:{}", deviceId,
                        mutilDeviceProperties.getBluWifi().getPushUrlPrefix() + "/bp", response);
                return false;
            }
        } catch (Exception e) {
            log.error(e, "推送血压失败");
        }
        return false;

    }
}
